{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100,2))\n",
    "X[:,0] = np.random.uniform(0.,100.,size=100)\n",
    "X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal(0,10.,size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGhtJREFUeJzt3X+MXWWdx/H3l+nATnGXKVINTKnFbFNEiNSdkK7dGLYYK6LSuP7AZVnWZdN/3BWRVMruZtHExJq6opsYkgZ02Q0REEkhaqyGQjZLAuvUFhFrVxakdKhSI4MGJnZavvvHPVdmpufce+6959zznOd8XkkzvXfu3Pvce2a+5znf5/s8j7k7IiJSfydV3QARESmGArqISCQU0EVEIqGALiISCQV0EZFIKKCLiERCAV1EJBIK6CIikVBAFxGJxJJhvtgZZ5zhq1atGuZLiojU3p49e37l7su7PW6oAX3VqlVMTU0N8yVFRGrPzJ7J8zilXEREIqGALiISCQV0EZFIKKCLiERCAV1EJBJDrXIREYnNzr3TbN91gOdmZjlrfIwtG9ewae1EJW1RQBcR6cH8AH7a2CgvHT3G3PHWzm/TM7PceO/jAJUEdaVcRERy2rl3mhvvfZzpmVkcmJmd+30wb5udO872XQcqaZ8CuohITtt3HWB27njXxz03MzuE1pxIAV1EJKe8gfqs8bGSW5JOAV1EJKc8gXpsdIQtG9cMoTUnUkAXEclpy8Y1jI2OLLhv9CRj2d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pgxt2PJbdjPN5vBI4AX0tSTbea2alEfKzdfRr4AnCQViB/EdhD/Me6LevYFh7fQg/olnJftGU5ZvYa4JvAJ9z9N1W3p0xm9h7geXffM//ulIfGdryXAG8FbnH3tcBLRJReSZPkjC8HzgHOAk6llW5YLLZj3U3hv++hB/RDwNnzbq8AnquoLaUys1FawfwOd783ufuX7Uuw5OvzVbWvBOuB95nZz2ml0jbQ6rGPJ5flEOfxPgQccvdHk9v30ArwMR/rdwBPu/sRd58D7gXeRvzHui3r2BYe30IP6D8AViej4SfTGki5v+I2FS7JHd8G7Hf3L8771v3A1cn/rwbuG3bbyuLuN7r7CndfReu47nb3K4EHgQ8kD4vqPQO4+y+AZ82svVv3JcBPiPhY00q1rDOzpcnvevs9R32s58k6tvcDf51Uu6wDXmynZvrm7kH/A94N/C/wf8A/Vd2ekt7jn9G61PoRsC/5925aOeUHgJ8lX0+vuq0lvf+LgW8l/38j8D/Ak8A3gFOqbl8J7/dCYCo53juBZbEfa+AzwE+BHwP/CZwS47EGvk5rnGCOVg/8mqxjSyvl8pUktj1OqwpooNfXTFERkUiEnnIREZGcFNBFRCKhgC4iEgkFdBGRSCigi4hEQgFdRCQSCugiIpFQQBcRicT/A0CiccCkrt1WAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11042a320>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:,0],X[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### demean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_demean = demean(X)# 均值归零"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     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gjeG3ApcC9wSp1POaXA28D7gRw9+fdfZYhftaNCPhmdhUw7e57ut6aAJ7oeH0wWFZHHwa+F3xd5+Ou87GFMrMlwHLgYeCV7n4IWhcF4Kzi9iwTX6LVcHsheP0KYLajYVPHn/drgBng60Eq6w4zeylD/Kxr8wAUM/sh8KqQt24FbgEuD/u2kGWVKlvqddzufl+wzq20UgB3t78tZP1KHXcPdT62E5jZy4BvAh9399+1Grz1ZGZXAk+5+04zu6S9OGTVuv28TwYuBD7m7g+b2e0MmaqrTcB397eHLTezC4BzgT3BH8NC4BEzu4hWa2BRx+oLgScz3tVURR13m5ldC1wJXObHanArf9w91PnYjmNmo7SC/d3u/q1g8W/M7Gx3PxSkJ58qbg9TtwK4yszeBfwB8HJaLf5xMzs5aOXX8ed9EDjo7g8Hr++lFfAH/lnXPqXj7o+6+1nuvsTdl9A6eRe6+6+BbcBfBNU6FwPPtG+R6sDMrgBuBq5y98Mdb20DrjGzl5jZubQ6rf+9iH3MwE+ApUHlxim0Oqe3FbxPqQty13cC+9z9ix1vbQOuDb6+Frgv733Liruvd/eFwd/xNcCD7v5B4EfAe4PVanXMAEGsesLMlgWLLgN+yhA/69q08If0XeBdtDotDwPXFbs7qfsy8BLg/uDuZoe7f8Td95rZFlq/NEeAG9z9aIH7mRp3P2JmHwW2AyPAP7r73oJ3KwsrgA8Bj5rZ7mDZLcAGYIuZXU+rIu3qgvYvTzcD95jZZ4FdBJ2bNfMx4O6gEfMYrVh1EgP+rDXSVkSkIWqf0hERkRYFfBGRhlDAFxFpCAV8EZGGUMAXEWkIBXwRkYZQwBcRaQgFfBGRhvh/y1Rs9cYgumAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11b4fe470>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_demean[:,0],X_demean[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2221335055073723e-14"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X_demean[:,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 梯度上升法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w,X):\n",
    "    return np.sum((X.dot(w)**2)) /len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def def_math(w,X):\n",
    "    return X.T.dot(X.dot(w))*2. /len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_debug(w,X, epsilon=0.00001):\n",
    "    res = np.empty(len(w))\n",
    "    for i in range(len(w)):\n",
    "        w_1 = w.copy()\n",
    "        w_1[i] += epsilon\n",
    "        w_2 = w.copy()\n",
    "        w_2[i] -= epsilon\n",
    "        res[i] = (f(w_1,X) -f(w_2,X))/(2*epsilon)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def direction(w):\n",
    "    return w /np.linalg.norm(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient_ascent(df, X, initial_w, eta,n_iters=1e4, epsilon=1e-8):\n",
    "    w = direction(initial_w)\n",
    "    cur_iter =0\n",
    "    \n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w,X)\n",
    "        last_w = w\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w)\n",
    "        if abs(f(w,X) - f(last_w,X)) < epsilon:\n",
    "            break\n",
    "            \n",
    "        cur_iter+=1\n",
    "        \n",
    "    return w\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "initial_w = np.random.random(X.shape[1]) # 初始向量\n",
    "eta = 0.001\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.76894104, 0.6393197 ])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradient_ascent(df_debug, X_demean, initial_w,eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAHnJJREFUeJzt3X+QXHWZ7/H3wzC4g3t1QILCJDhxzYYfokSnEI3X6/IrkcUkWiCg62aRkvIWenUXkQQUkMU1W1kJIGrdFNwt9hbeEIKEgO5GDFK1sBdwQhIQs1m5qJCB1YFkEiEBkvDcP/o06XROd5/uPqfPr8+rKpXp7jPn18w853ue7/P9HnN3RESk+A5IewdERKQ3FPBFREpCAV9EpCQU8EVESkIBX0SkJBTwRURKQgFfRKQkFPBFREpCAV9EpCQOTHsHah122GE+PDyc9m6IiOTK2rVrn3f3Sa2WiyXgm9kgcBPwLsCBzwKbgNuAYeA3wCfdfWuz9QwPDzM6OhrHLomIlIaZ/TbKcnGldK4H/sXdjwbeA2wEFgBr3H0asCZ4LSIiKek64JvZm4APAzcDuPur7j4BzAVuCRa7BZjX7bZERKRzcbTw3wGMA/9oZuvM7CYzeyPwVnd/DiD4//AYtiUiIh2KI+AfCLwX+L67zwBeoo30jZldaGajZjY6Pj4ew+6IiEiYOAL+ZmCzuz8cvF5B5QLwOzM7AiD4//dh3+zuS919xN1HJk1q2cksIiId6rpKx93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Fbg0bMU1Ri1QG0+bPWSFsRkZIofEpHREQqFPBFREpCAV9EpCQU8EVESkIBX0SkJBTwRURKQgFfRKQkFPBFREri/wN46oKtKLKzAwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11bcafd30>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w = gradient_ascent(def_math, X_demean, initial_w,eta)\n",
    "plt.scatter(X_demean[:,0],X_demean[:,1])\n",
    "plt.plot([0,w[0]*30],[0,w[1]*30],color='r') # 主成分\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
